<?php
/**
 * Created by PhpStorm.
 * User: 陈立强
 * Date: 2019/11/23
 * Time: 14:08
 */


namespace app\manage\controller;

use app\common\model\Orders AS OrdersModel;

use app\common\model\OrderGoods AS OrderGoodsModel;

use app\common\model\Goods AS GoodsModel;

use app\common\model\Members AS MembersModel;



class DataChart extends Base

{

    /*
     * 订单统计
     * */
    public function order(){
		$startTime       = input('get.start_time/s', '');

		$endTime         = input('get.end_time/s', '');
		$this->assign('startTime', $startTime);
		$this->assign('endTime', $endTime);
		return $this->fetch();

    }
	/*
		 * 订单统计
		 * */
	public function orderValue(){
		$startTime       = input('post.start_time', '');

		$endTime         = input('post.end_time', '');

		$where[] = [

			['pay_status', 'eq', '0'],

		];
		if ($startTime) {
			$where[] = [
				['create_time', 'egt', strtotime($startTime)],
			];
		}
		if ($endTime) {
			$where[] = [
				['create_time', 'lt', strtotime($endTime)],
			];
		}

		$orders = OrdersModel::field("FROM_UNIXTIME(create_time, '%Y-%m-%d')as chartdate,count(id) as tcount,sum(pay_amount) as money")
			->where($where)
			->group("chartdate")
			->limit(30)
			->select()->toArray();

		foreach ($orders as $val){

			$datearr[] = $val['chartdate'];

			$payarr[$val['chartdate']] = $val['money'];

		}
		$data = array(
			'总额'=>$payarr,
		);
		$series = [];//[订单=>[x,x,x,x],用户=>[x,x,x],
		foreach ($data as $groupname=>$val){
			$legend_data[] = $groupname;
			$one=[];
			$one['name'] = $groupname;
			$one['type'] = 'line';
			$one['stack'] = '数量';
			foreach ($datearr as $date){
				if(isset($val[$date])){
					$one['data'][] = $val[$date];
				}else{
					$one['data'][] = 0;
				}
			}
			$series[]=$one;
		}
		$data['chartdate'] = $datearr;
		$data['legend_data'] = $legend_data;
		$data['seriess'] = $series;
		return $data;
	}
    /*
     * 产品销量统计
     * */
    public function goods(){

        $goods = GoodsModel::field("id,goods_name,actual_sale_nums")
            ->order("actual_sale_nums desc")
            ->limit(30)
            ->select();

        $this->assign('goods', $goods);

        return $this->fetch();


    }


    /*
     * 会员统计
     * */
    public function member(){

        $startTime       = input('get.start_time/s', '');

        $endTime         = input('get.end_time/s', '');
        $where = [];
        if ($startTime) {

            $where[] = [

                ['create_time', 'egt', strtotime($startTime)],

            ];

        }

        if ($endTime) {

            $where[] = [

                ['create_time', 'lt', strtotime($endTime)],

            ];

        }

        $users = MembersModel::field("FROM_UNIXTIME(create_time, '%Y-%m-%d')as chartdate,count(id)as tcount")
            ->where($where)
            ->group("chartdate")
            ->select();
        $this->assign('startTime', $startTime);

        $this->assign('endTime', $endTime);

        $this->assign('users', $users);

        return $this->fetch();


    }


}
